package com.fit.exercise.msgp2p;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class ChatFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1647463931357400650L;
	private JPanel contentPane;
	private final JPanel panel_2 = new JPanel();
	private final JTextPane chatContent = new JTextPane();
	private final JSplitPane splitPane = new JSplitPane();
	private final JPanel panel_4 = new JPanel();
	private final JPanel panel_5 = new JPanel();
	private final JSplitPane splitPane_1 = new JSplitPane();
	private final JPanel panel = new JPanel();
	private final JPanel panel_1 = new JPanel();
	private final JTextArea textArea = new JTextArea();
	private final JButton btnSend = new JButton("Send");

	private PersonInfo chatFriend;
	private final JPanel panel_3 = new JPanel();
	private final JButton btnIntroduceFriend = new JButton("Introduce friend");

	/**
	 * Create the frame.
	 * 
	 * @param port
	 */

	public ChatFrame() {
		initGUI();
	}

	private void initGUI() {
		
		setMinimumSize(new Dimension(700, 450));
		setTitle("Chat Dialog");
		setPreferredSize(new Dimension(800, 500));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		splitPane.setResizeWeight(1.0);

		contentPane.add(splitPane, BorderLayout.CENTER);

		splitPane.setLeftComponent(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		splitPane_1.setResizeWeight(1.0);
		splitPane_1.setOrientation(JSplitPane.VERTICAL_SPLIT);

		panel_4.add(splitPane_1, BorderLayout.CENTER);

		splitPane_1.setLeftComponent(panel);
		panel.setLayout(new BorderLayout(0, 0));
		getChatContent().setEditable(false);
		panel.add(getChatContent());

		panel.add(panel_3, BorderLayout.SOUTH);
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.X_AXIS));

		panel_3.add(btnIntroduceFriend);
		btnIntroduceFriend.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				displayIntroduceFriendDialog();
			}
		});

		splitPane_1.setRightComponent(panel_1);
		textArea.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.isActionKey()) {
					// sendMessage();
				}
			}
		});

		panel_1.add(textArea);
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				sendMessage();
			}
		});
		btnSend.setMaximumSize(new Dimension(70, 50));
		btnSend.setMargin(new Insets(2, 2, 2, 2));
		btnSend.setSize(new Dimension(70, 50));
		btnSend.setPreferredSize(new Dimension(70, 50));

		panel_1.add(btnSend);
		panel_5.setMinimumSize(new Dimension(150, 10));
		panel_5.setPreferredSize(new Dimension(150, 10));

		splitPane.setRightComponent(panel_5);
		panel_5.setLayout(new BorderLayout(0, 0));
		panel_2.setPreferredSize(new Dimension(10, 150));
		panel_5.add(panel_2, BorderLayout.SOUTH);
		
		BufferedImage myPicture;
		try {
			myPicture = ImageIO.read(new File("a1.png"));
			JLabel picLabel = new JLabel(new ImageIcon(myPicture));
			panel_2.add(picLabel);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}

	private void sendMessage() {
		try {
			UDPUtil.sendMessage(textArea.getText(), chatFriend,
					Packet.TYPE_CHAT_MSG, Packet.FLAG_DATA, null);
			StyledDocument doc = chatContent.getStyledDocument();
			SimpleAttributeSet receiverKeyword = new SimpleAttributeSet();
			StyleConstants.setBold(receiverKeyword, true);
			StyleConstants.setForeground(receiverKeyword, Color.ORANGE);
			doc.insertString(doc.getLength(), "Me: ", receiverKeyword);
			doc.insertString(doc.getLength(), textArea.getText() + "\n", null);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(btnSend, "Can not send message");
		}
	}

	public PersonInfo getChatFriend() {
		return chatFriend;
	}

	public void setChatFriend(PersonInfo chatFriend) {
		this.chatFriend = chatFriend;
	}

	public JTextPane getChatContent() {
		return chatContent;
	}

	@Override
	public boolean equals(Object obj) {

		if (obj == this)
			return true;

		if (!(obj instanceof ChatFrame))
			return false;

		ChatFrame chatFrame = (ChatFrame) obj;
		return this.chatFriend == null ? chatFrame.chatFriend == null
				: this.chatFriend.equals(chatFrame.chatFriend);
	}

	private void displayIntroduceFriendDialog() {
		FriendsList_DlgAdd dlg = new FriendsList_DlgAdd() {
			@Override
			protected void sendData(JTable table) {
				{
					int selectionID = table.getSelectedRow();
					try {

						UDPUtil.sendMessage(Util
								.convertToByteArray(T2Messenger.friendList
										.get(selectionID)), chatFriend,
								Packet.TYPE_ADD_FRIEND_MSG, Packet.FLAG_DATA,
								null);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		};
	}
}
